<template>
  <el-drawer v-model="drawerVisible" :z-index="1000" :destroy-on-close="true" size="500px" :title="`${drawerProps.title}`">
    <el-form
      ref="MeterFormRef"
      label-width="130px"
      label-suffix=" :"
      :rules="rules"
      :disabled="drawerProps.isView"
      :model="drawerProps.row"
      :hide-required-asterisk="drawerProps.isView"
    >
      <el-form-item label="工位编号" prop="stationNo">
        <el-input v-model="drawerProps.row!.stationNo" placeholder="请填写工位编号" clearable></el-input>
      </el-form-item>
      <el-form-item label="所属装置" prop="deviceId">
        <el-select v-model="drawerProps.row!.deviceId" placeholder="请选择所属装置" clearable>
          <el-option v-for="item in deviceSelList" :key="item.deviceId" :label="item.deviceName" :value="item.deviceId" />
        </el-select>
      </el-form-item>

      <el-form-item label="名称" prop="meterName">
        <el-input v-model="drawerProps.row!.meterName" placeholder="请填写名称" clearable></el-input>
      </el-form-item>

      <el-form-item label="量程开始" prop="rangeStart">
        <el-input v-model="drawerProps.row!.rangeStart" placeholder="请填写量程开始" clearable></el-input>
      </el-form-item>

      <el-form-item label="量程结束" prop="rangeEnd">
        <el-input v-model="drawerProps.row!.rangeEnd" placeholder="请填写量程结束" clearable></el-input>
      </el-form-item>

      <el-form-item label="读取测量值" prop="readMeasurement">
        <el-input v-model="drawerProps.row!.readMeasurement" placeholder="请填写读取测量值" clearable></el-input>
      </el-form-item>

      <el-form-item label="备注" prop="remark">
        <el-input v-model="drawerProps.row!.remark" placeholder="请填写备注" clearable></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="_colse">取消</el-button>
      <el-button type="primary" v-show="!drawerProps.isView" @click="handleSubmit">确定</el-button>
    </template>
  </el-drawer>
</template>

<script setup lang="ts" name="MeterDrawer">
import { ref, reactive } from "vue";
import {} from "@/utils/serviceDict";
import { ElMessage, FormInstance } from "element-plus";
import { Meter, ResultData } from "@/api/interface";

import { getCabinetSelList } from "@/api/modules/deviceManage/cabinet";

const rules = reactive({
  stationNo: [
    { required: true, message: "请填写工位编号" },
    {
      validator(rule: any, value: any, callback: Function) {
        if (Number.isInteger(Number(value))) {
          callback();
        } else {
          callback(new Error("工位编号必须由数字组成"));
        }
      }
    }
  ],
  deviceId: [{ required: true, message: "请选择设备分类" }],
  meterName: [{ required: true, message: "请填写名称" }]
});

const deviceSelList: any = ref([]);
const searchParam = ref({});

interface DrawerProps {
  title: string;
  isView: boolean;
  row: Partial<Meter.ResMeterList>;
  api?: (params: any) => Promise<any>;
  getTableList?: () => void;
  resetTableList?: () => void;
}

const drawerVisible = ref(false);
const drawerProps = ref<DrawerProps>({
  isView: false,
  title: "",
  row: {}
});

// 接收父组件传过来的参数
const acceptParams = async (params: DrawerProps) => {
  drawerProps.value = params;
  drawerVisible.value = true;
  try {
    const { result } = (await getCabinetSelList({ pageNum: 1, pageSize: 100000000 })) as ResultData;
    deviceSelList.value = result.dataList;
  } catch (error) {}
};

// 提交数据（新增/编辑）
const MeterFormRef = ref<FormInstance>();
/**
 * 关闭弹窗
 */
const _colse = () => {
  drawerVisible.value = false;
  MeterFormRef.value && MeterFormRef.value.resetFields();
};
/**
 * 提交信息
 */
const handleSubmit = () => {
  MeterFormRef.value!.validate(async valid => {
    if (!valid) return;
    searchParam.value = { deviceId: drawerProps.value.row.deviceId };
    try {
      await drawerProps.value.api!({ ...drawerProps.value.row });
      ElMessage.success({ message: `${drawerProps.value.title}成功！` });
      drawerProps.value.title === "新增" ? drawerProps.value.resetTableList!() : drawerProps.value.getTableList!();
      drawerVisible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};
defineExpose({
  searchParam,
  acceptParams
});
</script>
